import { MergeBillApi } from '@/api/MergeBill.api.js'
import { Common } from '@/utils/common.js'
import config from '@/config'
const baseUrl = process.env.NODE_ENV === 'development' ? config.baseUrl.dev : config.baseUrl.pro.replace('/api', '/')

export default {
  name: '',

  data () {
    return {
      isShowTableMingxihuizongModal: false,
      companyName: null,
      companyId: null,
      year: null,
      month: null,
      billDate: null,
      tableData: [],
      tableComone: {
        pageIndex: 1, // 页码
        totalRows: 0, // 总条数
        pageSize: 10 // 当前页面展示条数
      },
      formItem: [
        { text: '社保合计', key: 'socialSecurityAmt', value: '' },
        { text: '公积金合计', key: 'providentFundAmt', value: '' },
        { text: '商保合计', key: 'commercialInsuranceAmt', value: '' },
        { text: '薪资合计', key: 'salaryAmt', value: '' },
        { text: '服务费合计', key: 'serviceFeeAmt', value: '' },
        { text: '总合计', key: 'totalAmt', value: '' }
      ],
      ignoreInsureNo: 'false',
      arrListDown: {
        ignoreInsureNoOps: [
          {label: '显示', value: 'false'},
          {label: '隐藏', value: 'true'}
        ]
      }
    }
  },

  watch: {
  },

  mounted () {
  },

  methods: {
    /**
     * 分页页码变动
     * @param {Number} val pageIndex
     */
    onChangePage (val) {
      this.tableComone.pageIndex = val
      this.query()
    },
    /**
     * 分页页码变动
     * @param {Number} val pageSize
     */
    onpageSizeChange (val) {
      this.tableComone.pageIndex = 1
      this.tableComone.pageSize = val
      this.query()
    },
    /**
     * 分页查询接口
     * @param {Object} obj {pageIndex, pageSize}
     */
    async query () {
      var params = {
        CompanyIds: this.companyId.join(','),
        // Category: 1,
        Month: this.billDate,
        IgnoreInsureNo: this.ignoreInsureNo,
        HasPaging: true,
        PageIndex: this.tableComone.pageIndex,
        PageSize: this.tableComone.pageSize
      }
      let res = await MergeBillApi.getSummaryDetailList(params)
      console.log('明细汇总列表res: ', res)
      if (res.status === 200 && res.data.success) {
        this.tableData = res.data.data
        this.tableComone.totalRows = res.data.totalRows
        this.companyName = res.data.customerNames
        this.tableData.map(row => {
          this.groupEach(row.socialInsurance, row, 'socialInsurance')
          this.groupEach(row.providentFund, row, 'providentFund')
          this.groupEach(row.commercialInsurance, row, 'commercialInsurance')
        })

        this.formItem.map(col => {
          col.value = res.data.summary[col.key]
          console.log('col: ', col)
        })
      } else {
        this.tableData = []
        this.$Message.error({
          content: res.data.message,
          duration: 10,
          closable: true
        })
      }
      this.loading = false
    },

    groupEach (group, row, groupName) {
      if (group) {
        row[groupName + 'totalAmt'] = group.totalAmt
        row[groupName + 'insureNo'] = group.insureNo
        row[groupName + 'companyTotalAmt'] = group.companyTotalAmt
        row[groupName + 'employeeTotalAmt'] = group.employeeTotalAmt

        var sumStr = ''
        Object.keys(group.items).forEach(insurance => {
          Object.keys(group.items[insurance]).forEach(cm => {
            Object.keys(group.items[insurance][cm]).forEach(key => {
              sumStr = insurance + cm + key
              row[sumStr] = group.items[insurance][cm][key]
            })
          })
        })
      }
    },
    
    ok () {
    },

    cancel () {
    },

    handleExportData () {
    },

    parsingItems (row, item, oneTitle, twoTitle1, twoTitle2, type) {
      var sumTitle = oneTitle + twoTitle1 + type
      console.log('sumTitle: ', sumTitle)
      row[sumTitle] = item[twoTitle1][type]
    },

    // 导出
    async exportSummaryDetailList () {
      var params = {
        CompanyIds: this.companyId.join(','),
        // Category: 1,
        Month: this.billDate,
        IgnoreInsureNo: this.ignoreInsureNo,
        HasPaging: true,
        PageIndex: this.tableComone.pageIndex,
        PageSize: this.tableComone.pageSize
      }
      console.log('导出params: ', params)
      let res = await MergeBillApi.exportSummaryDetailList(params)
      if (res.status === 200 && res.data.success) {
        console.log('❤ 返回链接: ', baseUrl + res.data.data)
        window.open(baseUrl + res.data.data, '_blank')
      } else {
        this.$Message.error({
          content: res.data.message,
          duration: 10,
          closable: true
        })
      }
    }
  }
}
